<?php 
 	include "database.php";
    //Za delovanje seje na vseh nivojih
    session_set_cookie_params(0,"/");
    session_start();

    if(!isset($_SESSION['username'])) {
        header("Location: login.php");
    }
    
    $usr_id = $_SESSION['id'];
    $queryProject = "SELECT current_project FROM user WHERE id = '$usr_id'";
    $resProject = executeQuery($queryProject);
    $row = mysql_fetch_array($resProject);
    $curr_proj = $row["current_project"];
?>

<!DOCTYPE html>

<html>
	<head>
	    <meta charset="utf-8">
		
		<!-- Font weight: light (300) & semibold (600) -->
		<link href='http://fonts.googleapis.com/css?family=Open+Sans:600,300' rel='stylesheet' type='text/css'>
		<link rel="stylesheet" href="css/default.css"/>
		<link rel="stylesheet" href="css/tabs.css"/>
		<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
		
		<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.10.2.custom.min.js"></script>
		<script type="text/javascript" src="js/actions.js"></script>
		
		<title>Scrum | My Tasks</title>		
	</head>
	
	<body>
	
		<span id="username"><?php echo $_SESSION['username']  ?></span>
		<span id="separator">|</span>
		<span id="role"><a href="edit_profile.php">edit</a></span>
		
		<h1>Scrum</h1>
		
		<menu>
			<li><a href="control_panel.php">control<span>Panel</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="project_wall.php">project<span>Wall</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="product_backlog.php">product<span>Backlog</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="sprint_backlog.php">sprint<span>Backlog</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a class="active" href="my_tasks.php">my<span>Tasks</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="progress_report.php">progress<span>Reports</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="login.php">logout</a></li>
		</menu>
		
		<section>
		
<!--			<div id="task_work" title="Work history">
				<table class="tasks_table">
					<thead>
						<td>date</td><td>work spent</td><td>hours remaining</td>
					</thead>
					<tr>
						<td>23.5.2013</td><td><input class="work_hours_input" type="number" value="2"/></td><td><input class="work_hours_input" type="number" value="0"/></td>
					</tr>
					<tr>
						<td>23.5.2013</td><td><input class="work_hours_input" type="number" value="2"/></td><td><input class="work_hours_input" type="number" value="2"/></td>
					</tr>
					<tr>
						<td>23.5.2013</td><td><input class="work_hours_input" type="number" value="0"/></td><td><input class="work_hours_input" type="number" value="4"/></td>
					</tr>
				</table>
			</div>
-->			
			<div id="task_work" title="Work history">
				<!--<table class="tasks_table">
					<thead>
						<td>date</td><td>work spent</td><td>hours remaining</td>
					</thead>
					<tr>
						<td>23.5.2013</td><td><input class="work_hours_input" type="number" value="2"/></td><td><input class="work_hours_input" type="number" value="0"/></td>
					</tr>
					<tr>
						<td>23.5.2013</td><td><input class="work_hours_input" type="number" value="2"/></td><td><input class="work_hours_input" type="number" value="2"/></td>
					</tr>
					<tr>
						<td>23.5.2013</td><td><input class="work_hours_input" type="number" value="0"/></td><td><input class="work_hours_input" type="number" value="4"/></td>
					</tr>
				</table>-->
			</div>
		
			<div id="all_stories" class="stories_container">
				
				<?php 
				
				$queryStoryId = "SELECT DISTINCT story_id FROM task WHERE assigned_id='$usr_id'";
				$queryStoryIdResult = executeQuery($queryStoryId);
				
				while ($row = mysql_fetch_array($queryStoryIdResult)){
					$story_id =		$row['story_id'];
					
					$queryStoryInfo = "SELECT * FROM story WHERE story_id='$story_id' AND project_id=$curr_proj;";
					$queryStoryInfoResult = executeQuery($queryStoryInfo);
					
					while ($row2 = mysql_fetch_array($queryStoryInfoResult)){
						$story_title = 	$row2['story_title'];
						$story_text = 	$row2['story_text'];	
						
						?>
						
						<div class="story">
							<input type="hidden" name="story_id" value=""<?=$story_id?>" />
								<div class="header">
									<h5><?=$story_title?></h5><br/>
								</div>
								<p><?=$story_text?></p>
								
								<table class="tasks_table my_tasks">
									<thead>
										<td>tasks</td><td>work</td>
									</thead>
									
										<?php
										$queryUserTasks = "SELECT * FROM task WHERE assigned_id='$usr_id' AND story_id='$story_id';";	
										$queryUserTasksResult = executeQuery($queryUserTasks);
										
										while ($row = mysql_fetch_array($queryUserTasksResult)) {
											$task_id = 		$row['task_id'];
											$story_id =		$row['story_id'];
											$task_title =	$row['task_title'];
											$task_status = 	$row['task_status'];
											$task_time =	$row['task_time'];
										?>
										<tr>
											<input type="hidden" name="task_id" value="<?=$task_id?>" />
											<input type="hidden" name="usr_id" value="<?=$usr_id?>" />
											<td><?=$task_title?> 
											<?php 
											
												$today = date("Y-m-d");
												
												$queryComplete = "SELECT * FROM task_work WHERE task_id=$task_id AND hours_remaining=0 ORDER BY date ASC";
												$queryCompleteResult = executeQuery($queryComplete);
												$row = mysql_fetch_array($queryCompleteResult);
												
												if(mysql_num_rows($queryCompleteResult)>0){
													 $queryTest = "UPDATE task_work SET hours_remaining=0 WHERE task_id=$task_id AND (date BETWEEN '$row[1]' AND '$today')";
													 executeQuery($queryTest);
													 
													 $tomorrow = strtotime($row[1]) + 86400;
													 $tomorrow = date("Y-m-d", $tomorrow);
													 $queryTest = "UPDATE task_work SET hours_spent=0 WHERE task_id=$task_id AND (date BETWEEN '$tomorrow' AND '$today')";
													 executeQuery($queryTest);
													
													
													$queryComplete = "UPDATE task SET task_status=2 WHERE task_id=$task_id";
													executeQuery($queryComplete);
													 
											?><span class="active">(completed)</span><?php
											 } else {
											 	$queryComplete = "UPDATE task SET task_status=1 WHERE task_id=$task_id";
												executeQuery($queryComplete);
											 }?>
											
											</td><td><a href="#" class="show_work_time">show</a></td>
										</tr>	
											
										<?php
										}
										?>
								</table>
								
						</div>		
						<?php 
						
					}
					
				}
				
				?>
		
		</section>
		
	</body>
</html>